<template>
  <el-card shadow="hover">
    <!--    查询条件部分-->
    <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">查询条件</span></el-divider>
    <div>
      <el-form ref="showForm" label-position="right" label-width="100px">

        <el-row :gutter="24">
          <el-col :span="10">
            <el-form-item label="住户名称&通知内容：" label-width="150px">
              <el-input v-model="queryParma.tzxx" clearable size="small" placeholder="请输入查询的住户名称&通知内容" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label-width="120px" label="通知消息状态：">
              <el-select v-model="queryParma.tzzt" style="position: relative;left: 5px" filterable default-first-option placeholder="请选择通知消息状态">
                <el-option
                  key="111"
                  label=""
                  value=""
                />
                <el-option
                  v-for="item in noticeInfoStatusList"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label="小区名称："
              style="display: inline-block"
            >
              <el-input :value="queryParma.xqmc" size="small" placeholder="请点击选择的房屋小区进行查询" @click.native="showVillageDialog" />
            </el-form-item>
            <span class="refresh_Icon"><i class="el-icon-circle-close" @click="clearQueryParams" /></span>
          </el-col>
          <el-col :span="4" align="right">
            <el-button type="primary" :loading="searchloding" @click="getNoticeInfoListPageHandle">查询</el-button>
            <el-button @click="resetForm('showForm')">重置</el-button>
            <a style="font-size:14px; margin-left: 10px" @click="toggleSearchStatus = !toggleSearchStatus">
              <template v-if="!toggleSearchStatus">
                展开
                <i class="el-icon-arrow-down" />
              </template>
              <template v-else>
                收起
                <i class="el-icon-arrow-up" />
              </template>
            </a>
          </el-col>
        </el-row>
        <template v-if="toggleSearchStatus">
          <el-row :gutter="24">
            <el-col :span="10">
              <el-form-item label="通知时间范围：" label-width="150px">
                <el-date-picker
                  v-model="timeArray.tzsjArray"
                  type="daterange"
                  align="right"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                  :default-time="['00:00:00', '23:59:59']"
                  @change="setChangeTimeHandle('tzsj')"
                />
                <span>
                  <i style="margin-left: 5px;font-size: 18px;cursor: pointer" class="el-icon-circle-close" @click="setNoneByType('tzsj')" />
                </span>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
      </el-form>
    </div>
    <!--    查询查询部分-->
    <div>
      <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">通知信息展示</span></el-divider>
      <!--      数据操作按钮-->
      <div style="padding: 10px" align="right">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDeleteBatch">批量删除</el-button>
        </el-button-group>
      </div>

      <el-table
        v-loading="loading"
        :data="dataSource"
        :border="true"
        align="center"
        empty-text="暂无数据"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column
          type="index"
          :index="indexMethod"
          fixed="left"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          label="住户名称"
          fixed="left"
          min-width="50"
          :header-align="columnsOptions.headerAlign"
          prop="householdName"
        />
        <el-table-column
          label="住户地址"
          fixed="left"
          min-width="200"
          :header-align="columnsOptions.headerAlign"
        >
          <template slot-scope="{row}">
            {{ getDictsValue(row.cotyCode,row) }}
          </template>
        </el-table-column>
        <el-table-column
          label="通知时间"
          min-width="100"
          align="right"
          :header-align="columnsOptions.headerAlign"
          prop="noticeTime"
        />
        <el-table-column
          label="通知内容"
          min-width="100"
          align="left"
          :header-align="columnsOptions.headerAlign"
          prop="noticeContent"
        />
        <el-table-column
          label="通知有效期"
          :header-align="columnsOptions.headerAlign"
          align="right"
          prop="noticeEffectiveDays"
        >
          <template slot-scope="{row}">
            {{ row.noticeEffectiveDays }} 天
          </template>
        </el-table-column>
        <el-table-column
          label="通知状态"
          :header-align="columnsOptions.headerAlign"
          align="center"
          prop="noticeStatus"
        >
          <template slot-scope="{row}">
            {{ getNoticeStatusDictsValue(row.noticeStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="注册时间"
          min-width="100"
          :header-align="columnsOptions.headerAlign"
          align="center"
          prop="createTime"
        />
        <el-table-column
          label="更新时间"
          min-width="100"
          :header-align="columnsOptions.headerAlign"
          align="center"
          prop="updateTime"
        />
        <el-table-column
          label="是否失效通知"
          align="center"
          fixed="right"
          :header-align="columnsOptions.headerAlign"
          min-width="80"
          prop="valiFlag"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.valiFlag"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchHandle(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="120"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.valiFlag === '0'"
              style="margin-left: 10px"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              :disabled="scope.row.valiFlag === '0'"
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div align="center">
        <div style="display: inline-block;width: 60px;height:60px;line-height: 60px;text-align: center;font-size: 15px">
          <span style="text-align: center;line-height: 60px;width: 60px;height:60px;">共{{ dataTotals }}条</span>
        </div>
        <div style="display: inline-block">
          <el-pagination
            :current-page.sync="queryParma.pageIndex"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryParma.pageSize"
            layout="total, sizes, prev,next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <modal
      ref="modals"
      :notice-info="modifyData"
      :notice-info-status-list="noticeInfoStatusList"
      @reflushDataHandle="getNoticeInfoListPageHandle"
    />
    <village-info-modal
      ref="villageinfomodals"
      @putSeletElementRow="getElementRowData"
    />
  </el-card>
</template>

<script>
import { dropBatchNoticeInfo, getAllNoticeInfoPageFilter, modifyOneNoticeInfo } from '@/api/ht/xqzhgl/tzgl/tzxxgl'
const { getDictsList, getDictsByType, getParentDictsList, getTreeDictsByType } = require('@/api/dicts')
const { Message } = require('element-ui')
import Modal from '@/views/ht/xqzhgl/tzxxgl/components/modal'
import VillageInfoModal from '@/views/ht/xqzhgl/tzxxgl/components/villageInfomodal'
export default {
  name: 'Index',
  components: {
    Modal,
    VillageInfoModal
  },
  data() {
    return {
      loading: false,
      columnsOptions: { // 表单展示
        headerAlign: 'center' // 字段lable 位置
      },
      timeArray: { // 时间数组对象
        tzsjArray: []
      },
      toggleSearchStatus: false, // 是否展开查询条件
      dataSource: [], // 信息列表
      dataTotals: 0, // 查询总条数
      modifyData: {}, // 待修改对象信息
      searchloding: false, // 搜索按钮加载
      queryParma: {
        pageIndex: 1,
        pageSize: 5,
        tzxx: '',
        tzzt: '',
        tzsjks: '',
        tzsjjs: '',
        xqbh: '',
        xqmc: ''
      },
      hasselectedIdArray: [], // 已经选中的信息ID数组
      noticeInfoStatusList: [], // 消息通知状态列表
      allCotyData: [], // 获取所有字典信息列表
      Dictdata: [], // 获取二级字典信息列表
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  mounted() {
    this.getNoticeInfoListPageHandle() // 获取信息列表
    this.getNoticeStatusDictListHandle() // 获取通知状态列表
    this.getDictArrayListHandle()
    this.getAllDictCotyTypeDataHandle()
  },
  methods: {
    getDictArrayListHandle() {
      this.getDictArrayList()
    },
    getAllDictCotyTypeDataHandle() {
      this.getAllDictCotyType()
    },
    getNoticeStatusDictListHandle() {
      this.getNoticeStatusDictList()
    },
    async getNoticeStatusDictList() {
      const res = await getDictsByType('notice_status')
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.noticeInfoStatusList = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async getAllDictCotyType() {
      const res = await getDictsByType('coty_type')
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.allCotyData = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async getDictArrayList() {
      const res = await getTreeDictsByType('coty_type')
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.Dictdata = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    getDictsValue(code, row) {
      const parentStr = getParentDictsList(this.Dictdata, code)
      const kidStr = getDictsList(this.allCotyData, code)
      const resultStr = parentStr + ' - ' + kidStr + ' - ' + row.villageName + ' - ' + row.buildingNumber + ' - ' + row.houseNumber
      return resultStr
    },
    getNoticeStatusDictsValue(code) {
      return getDictsList(this.noticeInfoStatusList, code)
    },
    getNoticeInfoListPageHandle() {
      this.getNoticeInfoListPage()
    },
    async getNoticeInfoListPage() {
      this.loading = true
      const res = await getAllNoticeInfoPageFilter(this.queryParma)
      const data = res.data.main.dataArray
      const resp = res.data.respOutMsgHeader
      const totals = res.data.main.totalsData
      const currentPage = res.data.main.currentPage
      if (resp.respCode === 200) {
        setTimeout(() => {
          this.searchloding = false
          this.loading = false
        }, 500)
        this.dataSource = data
        this.dataTotals = totals
        this.queryParma.pageIndex = currentPage
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.loading = false
      }
    },
    clearQueryParams() {
      this.queryParma.xqbh = ''
      this.queryParma.xqmc = ''
    },
    showVillageDialog() {
      this.$refs.villageinfomodals.setShowVillageDilogVisible()
    },
    getElementRowData(row) {
      // 视图set值检测并更新视图
      this.$set(this.queryParma, 'xqmc', row.villageName)
      // 赋用于实际传参的值
      this.queryParma.xqbh = row.id
    },
    indexMethod(index) {
      return index + 1 + (this.queryParma.pageIndex - 1) * this.queryParma.pageSize
    },
    handleAdd() {
      this.$refs.modals.checkModelType('add')
    },
    handleEdit(index, row) {
      // 使用父子传参方式
      this.modifyData = row
      this.$refs.modals.checkModelType('edit')
    },
    resetForm(formName) {
      // this.$refs[formName].resetFields()
      this.queryParma = {
        pageIndex: 1,
        pageSize: 5,
        tzxx: '',
        tzzt: '',
        tzsjks: '',
        tzsjjs: '',
        xqbh: '',
        xqmc: ''
      }
    },
    handleDelete(index, row) {
      const rowIdarray = []
      rowIdarray.push(row.id)
      this.dropOneNoticeInfo(rowIdarray)
    },
    handleDeleteBatch() {
      this.dropBatchNoticeInfo()
    },
    async dropOneNoticeInfo(array) {
      const res = await dropBatchNoticeInfo(array)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getNoticeInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async dropBatchNoticeInfo() {
      const res = await dropBatchNoticeInfo(this.hasselectedIdArray)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getNoticeInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    switchHandle(row) {
      this.$confirm('如果设置无效操作，将会使得该房屋关联的绝大部分功能失效或者无法正常使用系统, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.modifyNoticeInfoHandle(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.getNoticeInfoListPageHandle()
      })
    },
    setNoneByType() {
      this.timeArray = {}
      this.queryParma.tzsjjs = ''
      this.queryParma.tzsjks = ''
    },
    setChangeTimeHandle() {
      this.queryParma.tzsjks = this.timeArray.tzsjArray[0]
      this.queryParma.tzsjjs = this.timeArray.tzsjArray[1]
    },
    handleSizeChange(val) {
      this.queryParma.pageSize = val
      this.getNoticeInfoListPageHandle()
    },
    handleCurrentChange(val) {
      this.queryParma.pageIndex = val
      this.getNoticeInfoListPageHandle()
    },
    handleSelectionChange(selections) {
      const rowIdArray = []
      for (const selection of selections) {
        rowIdArray.push(selection.id)
      }
      this.hasselectedIdArray = rowIdArray
    },
    modifyNoticeInfoHandle(row) {
      this.modifyNoticeInfo(row)
    },
    async modifyNoticeInfo(row) {
      const res = await modifyOneNoticeInfo(row)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.getNoticeInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    }
  }
}
</script>

<style scoped>
.refresh_Icon{
  margin-left: 5px;
  font-size: 20px;
  display: inline-block;
  cursor: pointer;
}
.refresh_Icon :hover{
  color: #20a0ff;
}
</style>
